<?php
/**
 * 为数据表查询插入提供
 */
ini_set('display_errors', '1');
error_reporting(E_ALL);
function __autoload($class){
	if(substr($class, 0, 3) == 'Kit'){
		$classPath = str_replace('_', '/', $class);
		$classPath .= '.php';
		include_once $classPath;
	}
}
$config = include_once 'config.php';
Kit_Db_Manager::setConfig($config['database']);
$adapter = Kit_Db_Manager::getAdapter('x01');
$tables = $adapter->queryAll('SHOW TABLE STATUS FROM kitchen');
$tb = $_GET['tb'];
?>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>辅助开发工具</title>
<style type="text/css">
li{
	float:left;
	padding-left:10px;
	height:25px;
	width:170px;
}
</style>
</head>
<body>
<h2>数据处理辅助工具</h2>
<table width="100%">
<tr>
<td><ol>
<?php 
$describe = '';
for($i=0; $i<count($tables); $i++){
	if(strpos($tables[$i]['Name'], 'kit', 0) > -1){
		if($tb == $tables[$i]['Name']){
			$describe = $tables[$i]['Comment'];
		}
		if($i%2 == 0){
			echo '<li><a href="?tb='.$tables[$i]['Name'].'" target="_self" title="'.$tables[$i]['Comment'].'">'.$tables[$i]['Name'].'</a></li>';
		}else {
			echo '<li style="background-color:#ccffcc;"><a href="?tb='.$tables[$i]['Name'].'" target="_self" title="'.$tables[$i]['Comment'].'">'.$tables[$i]['Name'].'</a></li>';
		}
	}
}
?>
</ol></td>
</tr>
</table>
<?php 
/**
 * 获取到
 * 
 * @return Kit_Dao_Abstract
 */
function getDao($daoClass){
	return new $daoClass;
}
$tbArray = array();
$insert = '';
$select = '';
$inArray = array();
if($tb != ''){
	$tmp = explode('_', $tb);
	array_shift($tmp);
	$tbArray = $tmp;
}
if($tbArray){
	$daoClass = 'Kit_Dao';
	foreach($tbArray as $val){
		$daoClass .= '_'.ucwords($val);
	}
	$dao = getDao($daoClass);
	$tbFields = $dao->getTableFields();
	
	$select .= 'SELECT ';
	$insert .= 'INSERT INTO '.$tb.'(';
	$fields = array();
	$values = array();
	foreach($tbFields as $val){
		array_push($fields, $val['Field']);
		if(strpos($val['Type'], 'int', 0)> -1){
			array_push($values, 'n');
			$inArray[$val['Field']] = 'number';
		}else{
			array_push($values, 's');
			$inArray[$val['Field']] = 'string';
		}
	}
	$fields = implode(',', $fields);
	$values = "'".implode("','", $values)."'";
	$select .= $fields.' FROM '.$tb.';';
	$insert .= $fields.') VALUES(';
	$insert .= $values.');';
}
?>
<h3><?php echo $tb;?>(<?php echo $describe;?>)</h3>
<table>
<tr>
<td>
插入SQL:<br /><br/>
<?php echo $insert;?><br /><br />
查询SQL:<br /><br/>
<?php echo $select?><br /><br />
插入函数调用的数组:<br />
<pre>
<?php 
$inSql = '';
if($inArray){
	$inSql .= '$record = array('."\n";
	foreach ($inArray as $key=>$val){
		$inSql .= "'".$key."'=>'".$val."',\n";
	}
	$inSql .= ');';
}
echo $inSql;
?>
</pre>
</td>
</tr>
</table>
</body>
</html>